const {createClient} = require('redis')
const getMessage = require('./db.js')

async function redisConnect() {
    const client = createClient({
        url: 'redis://localhost:6379',
        socket_keepalive: true,
        no_ready_check: true
    })
    client.on('error', (err) => console.log('Redis Client Error', err));
    await client.connect();
    return client
}

const msg_key = 'message_key'

async function getCachedMessage(client) {
    let msg = await client.get(msg_key)
    if (!msg) {
        msg = await getMessage()
        await client.set(msg_key, msg)
    }
    return msg
}

module.exports = {getCachedMessage, redisConnect}